package com.srwl.mytx.widget;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

import com.srwl.mytx.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class OfferDialog extends DialogFragment {
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String BID_INCREMENT = "param1";
    private static final String ARG_PARAM2 = "param2";


    @BindView(R.id.et_offer)
    EditText et_offer;
    @BindView(R.id.rg_offer)
    RadioGroup rg_offer;
    @BindView(R.id.rb_200)
    RadioButton rb_200;
    @BindView(R.id.rb_500)
    RadioButton rb_500;
    @BindView(R.id.rb_1000)
    RadioButton rb_1000;
    @BindView(R.id.rb_2000)
    RadioButton rb_2000;
    @BindView(R.id.btn_custom_offer)
    Button btn_custom_offer;
    @BindView(R.id.btn_quick_offer)
    Button btn_quickOffer;
    @BindView(R.id.tv_title)
    TextView tv_title;

    Integer offer = null;
    Integer bidIncrement = null;

    public OfferDialog() {
        // Required empty public constructor

    }


    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param bidIncrement Parameter 1.
     * @return A new instance of fragment OfferDialog.
     */
    // TODO: Rename and change types and number of parameters
    public static OfferDialog newInstance(Integer bidIncrement) {
        OfferDialog fragment = new OfferDialog();
        Bundle args = new Bundle();
        args.putInt(BID_INCREMENT, bidIncrement);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_offer_dialog, container, false);
        ButterKnife.bind(this, view);
        bidIncrement = getArguments().getInt(BID_INCREMENT);

        tv_title.setText("加价幅度:RMB" + bidIncrement);
        rb_200.setText(2*bidIncrement+"");
        rb_500.setText(3*bidIncrement+"");
        rb_1000.setText(4*bidIncrement+"");
        rb_2000.setText(5*bidIncrement+"");
        rg_offer.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {
                    case R.id.rb_200:
                        if (rb_200.isChecked()) {
                            offer = 200;
                            et_offer.setText(null);
                        }

                        break;
                    case R.id.rb_500:
                        if (rb_500.isChecked()) {
                            offer = 500;
                            et_offer.setText(null);
                        }

                        break;
                    case R.id.rb_1000:
                        if (rb_1000.isChecked()) {
                            offer = 1000;
                            et_offer.setText(null);
                        }
                        break;
                    case R.id.rb_2000:
                        if (rb_2000.isChecked()) {
                            offer = 2000;
                            et_offer.setText(null);
                        }
                        break;

                }
            }
        });
        et_offer.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO: 2016/11/9 0009 应该要过滤
                if (!TextUtils.isEmpty(et_offer.getText())) {
                    rg_offer.clearCheck();
                    offer = Integer.parseInt(et_offer.getText().toString());
                }
            }
        });
        btn_custom_offer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (offer == null) {
                    Toast.makeText(getActivity(), "还没有输入或者选择金额哦！", Toast.LENGTH_LONG).show();
                    return;
                }
                if (offer<bidIncrement){
                    Toast.makeText(getActivity(), "该拍品出价金额必须大等于 RMB"+bidIncrement+"！", Toast.LENGTH_LONG).show();
                    return;
                }
                mListener.offerClick(offer);
            }
        });
        btn_quickOffer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bidIncrement != null) {
                    mListener.offerClick(bidIncrement);
                }
            }
        });
        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {

        }
    }


    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnOfferClickListener {
        // TODO: Update argument type and name
        void offerClick(Integer offer);
    }

    private OnOfferClickListener mListener;

    public void setOnOfferClickListener(OnOfferClickListener mListener) {

        this.mListener = mListener;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // must be called before set content
        dialog.setContentView(R.layout.fragment_offer_dialog);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.setCanceledOnTouchOutside(true);

        // 设置宽度为屏宽、靠近屏幕底部。
        Window window = dialog.getWindow();
        // window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(wlp);
        window.setDimAmount(0f);
        return dialog;
    }

    @OnClick(R.id.back)
    void back(){
        dismiss();
    }
    @OnClick(R.id.close)
    void close(){
        dismiss();
    }

}
